/*
 * Copyright 2008 Neil Bacon
 * Distributed under the terms of the Mozilla Public License version 1.1
 * or the GNU General Public License version 3.
 */

package nb.xadd.node.impl;

import nb.util.Utils;
import nb.xadd.node.Node;

/**
 * Non-terminal decision node.
 */
public abstract class AbstractDecisionNode extends AbstractNode {
    private final Node trueChild;
    private final Node falseChild;

    protected AbstractDecisionNode(Node trueChild, Node falseChild) {
        super();
        this.trueChild = trueChild;
        this.falseChild = falseChild;
    }

    protected int calcHashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + trueChild.hashCode();
        result = prime * result + falseChild.hashCode();
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        AbstractDecisionNode other = (AbstractDecisionNode) obj;
        return trueChild == other.trueChild && falseChild == other.falseChild;
    }

    public Node getTrueChild() {
        return trueChild;
    }

    public Node getFalseChild() {
        return falseChild;
    }

    @Override
    public void appendTo(int depth, StringBuilder buf) {
        Utils.appendIndent(depth, buf);
        buf.append("trueChild:\n");
        trueChild.appendTo(depth + 1, buf);

        Utils.appendIndent(depth, buf);
        buf.append("falseChild:\n");
        falseChild.appendTo(depth + 1, buf);
    }
    
}
